<template>
  <div id="user-login">
    <h2>用户登录</h2>
    <a-form
      :model="form"
      :style="{ width: '600px' }"
      @submit="loginSubmit"
      label-align="left"
      auto-label-width
    >
      <a-form-item
        field="name"
        tooltip="用户名、手机号或者电子邮箱"
        label="用户名"
      >
        <a-input v-model="form.userAccount" placeholder="请输入用户名" />
      </a-form-item>
      <a-form-item field="post" label="密码">
        <a-input-password
          v-model="form.userPassword"
          placeholder="请输入密码"
        />
      </a-form-item>
      <a-form-item>
        <a-button html-type="submit" long status="success">登录</a-button>
      </a-form-item>
    </a-form>
    <a-button type="text" @click="goRegister">没有账号，前往注册</a-button>
  </div>
</template>

<style scoped>
#user-login h2 {
  text-align: center;
}
</style>

<script setup lang="ts">
import { reactive } from "vue";
import { UserControllerService, UserLoginRequest } from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";
import { useStore } from "vuex";

const form = reactive({
  userAccount: "",
  userPassword: "",
}) as UserLoginRequest;

const store = useStore();
const router = useRouter();
/**
 * 提交登录表单
 */
const loginSubmit = async () => {
  const res = await UserControllerService.userLoginUsingPost(form);
  if (res.code === 0) {
    router.push({
      path: "/",
      replace: true,
    });
    store.dispatch("user/getLoginUser");
    message.success("登录成功" + res.message);
  } else {
    message.error("登录失败" + res.message);
  }
};

const goRegister = () => {
  router.push({
    path: "/user/register",
    replace: true,
  });
};
</script>
